 package cn.ccew.common.utils;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 
 public class MyInterceptor   implements HandlerInterceptor
 {
		   public void afterCompletion(HttpServletRequest arg0, HttpServletResponse request, Object arg2, Exception response)  throws Exception
		   {
		   }
		 
		   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception
		   {
			   
		   }
		 
		   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2)   throws Exception
		   {
		   		response.setContentType("text/html");
		   		response.setCharacterEncoding("UTF-8");
		   		request.setCharacterEncoding("UTF-8");
		
		   		String urlControl = request.getServletPath();
		   		Date dt = new Date();
		   		DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		   		String nowTime = df.format(dt);
		
		   		int flag=urlControl.indexOf("do");
		 
			    if (flag>=0) {
			    			System.out.println(request.getRemoteAddr());
			    			Object userid = request.getSession().getAttribute("userid");
			    			Object username = request.getSession().getAttribute("username");
			    			if (userid == null) {
			    					response.getWriter().print("<script type=\"text/javascript\" language=\"javascript\">alert('您会话超时,重新登录!'); location.href = \"login.html\";</script>");
	    							return false;
			    			}
					       else {
								     System.out.println(username.toString() + "操作" + urlControl + "\t\t" + nowTime);
								     return true;
					       }
			     }
			    else {
			    	 	System.out.println(urlControl + "\t\t" + nowTime);
			    	  return true;
			    }
			}	
}
